package org.wlgzs.shop.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.wlgzs.shop.bean.Msg;
import org.wlgzs.shop.bean.storeShow;
import org.wlgzs.shop.dao.GoodsDataMapper;
import org.wlgzs.shop.dao.storeShowMapper;
import org.wlgzs.shop.service.IShowStoreService;
import org.wlgzs.shop.service.IUserDateService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class showStoreServiceImpl implements IShowStoreService {
    @Autowired
    storeShowMapper storeShowMapper;

    @Autowired
    IUserDateService iUserDateService;

    @Autowired
    GoodsDataMapper goodsDataMapper;

    Logger logger= LoggerFactory.getLogger(showStoreServiceImpl.class);
    //删除的商品
    @Override
    public Msg delectGoods(int goodId){
        int num=goodsDataMapper.deleteByPrimaryKey(goodId);
        if(num!=0){
            return Msg.success();
        }
        return Msg.fail();
    }

    @Override
    public  String getStoreShow(int id) {
        storeShow show=storeShowMapper.selectByPrimaryKey(id);
        String date=null;
        if(show==null){
            return "|";
        }
        if(show.getHotone()!=null&&!show.getHotone().equals("")){
            date+=("|"+show.getHotone().split(",")[2]+"|");
        }
        else {date+="|";}
        if(show.getHottwo()!=null&&!show.getHottwo().equals("")){
            date+=(show.getHottwo().split(",")[2]+"|");
        }
        else {date+="|";}
        if(show.getHotthree()!=null&&!show.getHotthree().equals("")){
            date+=(show.getHotthree().split(",")[2]+"|");
        }
        else {date+="|";}
        logger.info("判断数据："+date);
        return date;
    }

    @Override
    public String[][] getStoreShowDate(int id) {
        storeShow show=storeShowMapper.selectByPrimaryKey(id);
        String[][] date=new String[3][];
        if(show==null){
            return date;
        }
        if(show.getHotone()!=null&&!show.getHotone().equals("")){
            date[0]=show.getHotone().split(",");
        }
        if(show.getHottwo()!=null&&!show.getHottwo().equals("")){
            date[1]=show.getHottwo().split(",");
        }

        if(show.getHotthree()!=null&&!show.getHotthree().equals("")){
            date[2]=show.getHotthree().split(",");
        }
        for(int d=0;d<3;d++){
            for(int j=0;j<date[d].length;j++){
                logger.info("推荐商品"+d+":"+date[d][j]);
            }
        }
        return date;
    }



    @Override
    public String upFile(MultipartFile file,HttpSession session,HttpServletRequest request) throws IOException {
        return iUserDateService.oneFilep(file,session,request);
    }



    //商品推荐
    @Override
    public Msg addShowGood(int storeId,String photo,String name,int id) {
        logger.info(storeId+"=="+photo+"=="+name+"=="+id);
        String data=photo+","+name+","+id;
        storeShow showRel= storeShowMapper.selectByPrimaryKey(storeId);
        if(showRel==null){
            showRel=new storeShow(storeId,0);
            storeShowMapper.insert(showRel);
        }
        storeShow show=new storeShow();
        show.setId(storeId);
        int number=showRel.getStatus();
        if(number==0){
            show.setStatus(1);
            show.setHotone(data);
        }
        if(number==1){
            show.setStatus(2);
            show.setHottwo(data);
        }
        if(number==2){
            show.setStatus(3);
            show.setHotthree(data);
        }
        if(number==3){
            show.setStatus(1);
            show.setHotone(data);
        }
        int num=storeShowMapper.updateByPrimaryKeySelective(show);
        if(num!=0) {
            return Msg.success();
        }
        return Msg.fail();
    }



    //多文件上传
    @Override
    public Msg  addHeadShow(MultipartFile[] files, HttpSession session, HttpServletRequest request, int id){
        String date= null;
        try {
            date = iUserDateService.allFileUp(files,session,request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String[] allDate=date.split("\\,");
        storeShow show=new storeShow();
        int number=allDate.length;
        if(0<number&&!allDate[0].equals("")){
            show.setHeadphotoone(allDate[0]);
        }
        if(1<number&&!allDate[1].equals("")){
            show.setHeadphototwo(allDate[1]);
        }
        if(2<number&&!allDate[2].equals("")){
            show.setHeadphotothree(allDate[2]);
        }
        if(3<number&&!allDate[3].equals("")){
            show.setHeadphotofour(allDate[3]);
        }
        if(4<number&&!allDate[4].equals("")){
            show.setHeadphotofive(allDate[4]);
        }
        int num=storeShowMapper.updateByPrimaryKeySelective(show);
        if(num!=0) {
            return Msg.success();
        }
        return Msg.fail();
    }



    //单文件上传
    @Override
    public Msg  addOneHeadShow(String name,String number,String storeId) {
        logger.info(name+"=="+number+"=="+storeId);
        int num=Integer.parseInt(number);
        storeShow show=new storeShow();
        show.setId(Integer.parseInt(storeId));
        if(num==1){
            show.setHeadphotoone(name);
        }
        if(num==2){
            show.setHeadphototwo(name);
        }
        if(num==3){
            show.setHeadphotothree(name);
        }
        if(num==4){
            show.setHeadphotofour(name);
        }
        if(num==5){
            show.setHeadphotofive(name);
        }
        int numberDate=storeShowMapper.updateByPrimaryKeySelective(show);
        if(numberDate!=0) {
            return Msg.success();
        }
        return Msg.fail();
    }
}
